package com.example.testdemo.resources.stage;

import java.util.ArrayList;
import java.util.List;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXObject;
import org.andengine.extension.tmx.TMXObjectGroup;
import org.andengine.extension.tmx.TMXProperties;
import org.andengine.extension.tmx.TMXTile;
import org.andengine.extension.tmx.TMXTileProperty;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.exception.TMXLoadException;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.region.ITextureRegion;

import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;

import com.example.testdemo.TankGameActivity;
import com.example.testdemo.ai.TrafficLaw;
import com.example.testdemo.manager.ResourcesManager;
import com.example.testdemo.resources.BaseResources;

public class BaseStageResources extends BaseResources {
    private final String TAG = BaseStageResources.class.getSimpleName();

    public TMXTile mHeartOnTile = null;
    public List<TMXTile> mTileGroundList = new ArrayList<TMXTile>();
    public List<TMXTile> mTileWallList = new ArrayList<TMXTile>();
    public List<TMXTile> mTileObstacleList = new ArrayList<TMXTile>();
    public List<TMXObject> mObjTankList = new ArrayList<TMXObject>();
    public List<TMXObject> mObjPlayerList = new ArrayList<TMXObject>();

    public TMXTiledMap mMap = null;
    public int mGroundId = -1;
    public int mWallId = -1;
    public TrafficLaw mTrafficLaw = null;
    public ITextureRegion mGroundTextureRegion = null;
    public ITextureRegion mWallTextureRegion = null;

    public Font mFont;
    public Music mMusicBackground;
    public Music mMusicWin;

    public BaseStageResources(TankGameActivity activity) {
        super(activity);
    }

    @Override
    public void load(int resources) {
        reloadFont(resources);
        reloadMusic(resources);
        reloadMap(resources);
    }

    public void reloadFont(int resources) {
        if (mFont != null) {
            mFont.unload();
        }
        mFont = FontFactory.create(mActivity.getFontManager(),
                mActivity.getTextureManager(), 256, 256,
                Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32,
                Color.WHITE);
        mFont.load();
    }

    public void reloadMap(int resources) {
        // clear list
        mTileGroundList.clear();
        mTileWallList.clear();
        mTileObstacleList.clear();
        mObjTankList.clear();
        mObjPlayerList.clear();

        if (mMap != null && mMap.getTMXLayers().get(0) != null) {
            mMap.getTMXLayers().get(0).dispose();
            mMap = null;
        }

        // TMX map
        final TMXLoader tmxLoader = new TMXLoader(mActivity.getAssets(),
                mActivity.getTextureManager(), TextureOptions.BILINEAR,
                mActivity.getVertexBufferObjectManager(),
                new TMXLoader.ITMXTilePropertiesListener() {
                    @Override
                    public void onTMXTileWithPropertiesCreated(
                            TMXTiledMap pTMXTiledMap, TMXLayer pTMXLayer,
                            TMXTile pTMXTile,
                            TMXProperties<TMXTileProperty> pTMXTileProperties) {
                        if (mGroundId == -1
                                && pTMXTileProperties != null
                                && pTMXTileProperties.containsTMXProperty(
                                        "type", "ground")) {
                            mGroundId = pTMXTile.getGlobalTileID();
                            mGroundTextureRegion = pTMXTile.getTextureRegion();

                        }

                        if (pTMXTileProperties == null
                                || pTMXTileProperties.containsTMXProperty(
                                        "type", "ground")) {
                            mTileGroundList.add(pTMXTile);
                        } else if (pTMXTileProperties != null
                                && pTMXTileProperties.containsTMXProperty(
                                        "type", "wall")) {
                            if (mWallId == -1) {
                                mWallId = pTMXTile.getGlobalTileID();
                                mWallTextureRegion = pTMXTile
                                        .getTextureRegion();
                            }
                            mTileWallList.add(pTMXTile);
                        } else if (pTMXTileProperties != null
                                && pTMXTileProperties.containsTMXProperty(
                                        "type", "obstacle")) {
                            mTileObstacleList.add(pTMXTile);
                        } else if (pTMXTileProperties != null
                                && pTMXTileProperties.containsTMXProperty(
                                        "type", "heart")) {
                            mHeartOnTile = pTMXTile;
                        }
                    }
                });
        try {
            switch (resources) {
            case ResourcesManager.RESOURCES_STAGE_1:
                mMap = tmxLoader.loadFromAsset("tmx/dry_land_1.tmx");
                break;
            }

            ArrayList<TMXObjectGroup> groupList = mMap.getTMXObjectGroups();
            if (groupList != null && groupList.size() > 0) {
                for (TMXObjectGroup group : groupList) {
                    for (TMXObject object : group.getTMXObjects()) {
                        if (object.getName().equals("player")) {
                            mObjPlayerList.add(object);
                        } else if (object.getName().equals("tank")) {
                            mObjTankList.add(object);
                        }
                    }
                }
            }

            mTrafficLaw = new TrafficLaw(mMap);
        } catch (TMXLoadException e) {
            Log.e(TAG, "load map failed !", e);
        }
    }

    public void reloadMusic(int resources) {
        if (mMusicBackground != null && !mMusicBackground.isReleased()) {
            mMusicBackground.release();
            mMusicBackground = null;
        }
        if (mMusicWin != null && !mMusicWin.isReleased()) {
            mMusicWin.release();
            mMusicWin = null;
        }

        try {
            mMusicBackground = MusicFactory.createMusicFromAsset(mActivity
                    .getEngine().getMusicManager(), mActivity,
                    "sounds/background.mp3");
            mMusicWin = MusicFactory.createMusicFromAsset(mActivity.getEngine()
                    .getMusicManager(), mActivity, "sounds/win.mp3");
        } catch (Exception e) {
            Log.e(TAG, "load music and sound failed !", e);
        }
    }

    @Override
    public void unload(int resources) {
        // clear list
        mHeartOnTile = null;
        mTileGroundList.clear();
        mTileGroundList = null;
        mTileWallList.clear();
        mTileWallList = null;
        mTileObstacleList.clear();
        mTileObstacleList = null;
        mObjTankList.clear();
        mObjTankList = null;
        mObjPlayerList.clear();
        mObjPlayerList = null;
        mMap = null;

        // unload resources
        mFont.unload();

        if (mMusicBackground != null && !mMusicBackground.isReleased()) {
            mMusicBackground.release();
            mMusicBackground = null;
        }
        if (mMusicWin != null && !mMusicWin.isReleased()) {
            mMusicWin.release();
            mMusicWin = null;
        }

        if (mMap != null && mMap.getTMXLayers().get(0) != null) {
            mMap.getTMXLayers().get(0).dispose();
            mMap = null;
        }
    }

}
